/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hk.shop.service.shop;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.modules.hk.shop.dao.shop.HkShopCollageCouponDao;
import com.thinkgem.jeesite.modules.hk.shop.dao.shop.HkShopCouponDao;
import com.thinkgem.jeesite.modules.hk.shop.dao.shop.HkShopLuckyBurstCouponDao;
import com.thinkgem.jeesite.modules.hk.shop.dao.shop.HkShopNewVipCouponDao;
import com.thinkgem.jeesite.modules.hk.shop.entity.shop.HkShopCollageCoupon;
import com.thinkgem.jeesite.modules.hk.shop.entity.shop.HkShopCoupon;
import com.thinkgem.jeesite.modules.hk.shop.entity.shop.HkShopLuckyBurstCoupon;
import com.thinkgem.jeesite.modules.hk.shop.entity.shop.HkShopNewVipCoupon;
import com.thinkgem.jeesite.modules.hk.sys.dao.user.HkUserDao;
import com.thinkgem.jeesite.modules.hk.sys.entity.user.HkUser;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * 优惠券Service
 *
 * @author 李欣荣
 * @version 2018-09-27
 */
@Service
@Transactional(readOnly = true)
public class HkShopCouponService extends CrudService<HkShopCouponDao, HkShopCoupon> {

    private static final String COUPON_STATE_WAIWAREHOUSE = "0"; //待促销
    private static final String COUPON_STATE_PROMOTION = ""; //仓库中
    private static final String COUPON_STATE_WAREHOUSE = "1"; //促销中


    @Autowired
    private HkUserDao hkUserDao;
    @Autowired
    private HkShopLuckyBurstCouponService hkShopLuckyBurstCouponService;
    @Autowired
    private HkShopNewVipCouponService hkShopNewVipCouponService;
    @Autowired
    private HkShopCollageCouponService hkShopCollageCouponService;

    @Override
    public HkShopCoupon get(String id) {
        return super.get(id);
    }

    @Override
    public List<HkShopCoupon> findList(HkShopCoupon hkShopCoupon) {
        return super.findList(hkShopCoupon);
    }

    @Override
    public Page<HkShopCoupon> findPage(Page<HkShopCoupon> page, HkShopCoupon hkShopCoupon) {
        return super.findPage(page, hkShopCoupon);
    }

    @Override
    @Transactional(readOnly = false)
    public void save(HkShopCoupon hkShopCoupon) {
        super.save(hkShopCoupon);
    }

    @Override
    @Transactional(readOnly = false)
    public void delete(HkShopCoupon hkShopCoupon) {
        super.delete(hkShopCoupon);
    }


    @Transactional(readOnly = false)
    public Boolean insert(HkShopCoupon hkShopCoupon) throws Exception {
        User manage = UserUtils.getUser();
        //查找HkUser乐币
        /* HkUser hkUser = hkUserDao.getHkUser(manage.getId());
            if (Double.parseDouble(hkUser.getIntegral()) <= 0 || hkUser.getIntegral() == null || ("").equals(hkUser.getIntegral())) {
            throw new Exception("用户乐币不足，请先充值！");
        }
        if (Double.parseDouble(hkUser.getIntegral()) >= Double.parseDouble(hkShopCoupon.getIntegral())) {
        */

        hkShopCoupon.setState(COUPON_STATE_WAIWAREHOUSE);
        super.save(hkShopCoupon);
        //根据折扣添加优惠券
        /*if (Double.parseDouble(hkShopCoupon.getDiscount()) < 8) {

            HkShopLuckyBurstCoupon hkShopLuckyBurstCoupon = new HkShopLuckyBurstCoupon();
            HkShopNewVipCoupon hkShopNewVipCoupon = new HkShopNewVipCoupon();
            //爆款活动
            hkShopLuckyBurstCoupon.setCoupon(hkShopCoupon); //优惠券
            hkShopLuckyBurstCoupon.setProductId(hkShopCoupon.getProduct().getId()); //商品
            hkShopLuckyBurstCoupon.setBeginTime(hkShopCoupon.getBeginTime());  //开始时间
            hkShopLuckyBurstCoupon.setEndTime(hkShopCoupon.getEndTime());  //结束时间
            hkShopLuckyBurstCoupon.setIntegral(hkShopCoupon.getIntegral()); //乐币
            hkShopLuckyBurstCoupon.setActivityStocks(hkShopCoupon.getStocks());  //活动库存
            hkShopLuckyBurstCoupon.setDiscount(hkShopCoupon.getDiscount()); //折扣
            hkShopLuckyBurstCoupon.setState(COUPON_STATE_WAIWAREHOUSE);
            hkShopLuckyBurstCouponService.save(hkShopLuckyBurstCoupon);

            //新人专享
            hkShopNewVipCoupon.setCoupon(hkShopCoupon); //优惠券
            hkShopNewVipCoupon.setProductId(hkShopCoupon.getProduct().getId()); //商品
            hkShopNewVipCoupon.setBeginTime(hkShopCoupon.getBeginTime());  //开始时间
            hkShopNewVipCoupon.setEndTime(hkShopCoupon.getEndTime());  //结束时间
            hkShopNewVipCoupon.setIntegral(hkShopCoupon.getIntegral()); //乐币
            hkShopNewVipCoupon.setActivityStocks(hkShopCoupon.getStocks());  //活动库存
            hkShopNewVipCoupon.setDiscount(hkShopCoupon.getDiscount()); //折扣
            hkShopNewVipCoupon.setState(COUPON_STATE_WAIWAREHOUSE);
            hkShopNewVipCouponService.save(hkShopNewVipCoupon);


        } else
        if (Double.parseDouble(hkShopCoupon.getDiscount()) >= 8 && Double.parseDouble(hkShopCoupon.getDiscount()) < 9) {
            //拼团
            HkShopCollageCoupon hkShopCollageCoupon = new HkShopCollageCoupon();
            hkShopCollageCoupon.setCoupon(hkShopCoupon); //优惠券
            hkShopCollageCoupon.setProductId(hkShopCoupon.getProduct().getId()); //商品
            hkShopCollageCoupon.setBeginTime(hkShopCoupon.getBeginTime());  //开始时间
            hkShopCollageCoupon.setEndTime(hkShopCoupon.getEndTime());  //结束时间
            hkShopCollageCoupon.setIntegral(hkShopCoupon.getIntegral()); //乐币
            hkShopCollageCoupon.setActivityStocks(hkShopCoupon.getStocks());  //活动库存
            hkShopCollageCoupon.setDiscount(hkShopCoupon.getDiscount()); //折扣hkShopCollageCouponDao.insert(hkShopCollageCoupon);
            hkShopCollageCoupon.setState(COUPON_STATE_WAREHOUSE);
            hkShopCollageCouponService.save(hkShopCollageCoupon);
            hkShopCoupon.setState(COUPON_STATE_WAREHOUSE);
        }
 */


        //扣减用户乐币
            /*Double d = Double.parseDouble(hkShopCoupon.getIntegral());
            hkUserDao.deduction(manage.getId(), d.intValue());*/
        /*} else {
            return false;
        }*/
        return true;
    }

    @Transactional(readOnly = true)
    public Page<HkShopCoupon> findCoupon(Page<HkShopCoupon> page, HkShopCoupon hkShopCoupon) {
        // 生成数据权限过滤条件（dsf为dataScopeFilter的简写，在xml中使用 ${sqlMap.dsf}调用权限SQL）
        hkShopCoupon.getSqlMap().put("dsf", dataScopeFilter(hkShopCoupon.getCurrentUser(), "o", "a"));
        // 设置分页参数
        hkShopCoupon.setPage(page);
        // 执行分页查询
        page.setList(dao.findList(hkShopCoupon));
        return page;
    }

    @Transactional(readOnly = true)
    public HkUser getHkUser(@Param("manageId") String managerId) {
        return hkUserDao.getHkUser(managerId);
    }

    @Transactional(readOnly = false)
    void addIntegral(String manageId, Double integral) {
        hkUserDao.addIntegral(manageId, integral);
    }


}